package com.infindo.frame.data;

 
import java.util.Vector;

@SuppressWarnings("unchecked")
public class DataProcTimer implements Runnable{
	
	private static final long waitTime = 1000;

    private Vector connectHandlerList  = new Vector(1);
	
    
    public DataProcTimer()
    {
    	new Thread(this).start();
    }
    
	public synchronized void addConnect(DataHandler addHandler)
	{
		   DataHandler adHandler;
		   int size = connectHandlerList.size();
	 	   for(int i = 0;i<size;i++)
		   {
	 		   adHandler = (DataHandler)connectHandlerList.elementAt(i);
			   if(adHandler.getDataHandlerNo()==addHandler.getDataHandlerNo())
			   {
                    return;
			   }
		   }
	 	   this.connectHandlerList.addElement(addHandler);
	}
	
	
	public synchronized boolean checkConnectQueue(int dataHandlerNo)
	{
		   DataHandler cdHandler;
		   int size = connectHandlerList.size();
	 	   for(int i = 0;i<size;i++)
		   {
	 		   cdHandler = (DataHandler)connectHandlerList.elementAt(i);
			   if(cdHandler.getDataHandlerNo()==dataHandlerNo)
			   {
				   cdHandler.stopConnect();
                   return true;
			   }
		   }
		   return false;
	}

	public void run() {
	   DataHandler tempHandler;
       while(true)
       {
	    	   int size = connectHandlerList.size();
	    	   //cancel
	    	   
	    	   //overTime
	    	   for(int i = 0;i<size;i++)
	    	   {
	    		   tempHandler = (DataHandler)connectHandlerList.elementAt(i);
	    		   if(tempHandler.isBolNeedTimer())
	    		   {
	    			   if(System.currentTimeMillis()-tempHandler.getWorkStartTime()>tempHandler.getSurvTime())
	    			   {
		    				tempHandler.stopConnect();
//		    				MyCanvas.setBolWaitingData(false);
//		    				if(tempHandler.isBolFirstTime()&& tempHandler.getLinkTime()<DataHandler.MaxLinkTime)
//		    				{
//		    					tempHandler.addLinkTime();
////		    					MyCanvas.dataProc.restartToProc(tempHandler);
//		    					if(tempHandler.getLinkTime()==0)
//		    						MyCanvas.bolWapGateWayProxy = true;
//		    					else if(tempHandler.getLinkTime()==DataHandler.MaxLinkTime-1)
//		    					{
//		    						tempHandler.setBolFirstTime(false);
//		    					}
//		    				}
	    			   }
	    		   }
	    	   }
	    	   //excute
	    	   for(int i = 0;i<connectHandlerList.size();i++)
	    	   {
	    		   tempHandler = (DataHandler)connectHandlerList.elementAt(i);
	    		   if(tempHandler.isBolStopWork())
	    		   {
    				   synchronized(this)
    				   {
    					   connectHandlerList.removeElementAt(i);
    					   i--;
    				   }
	    		   }
	    	   }

	    	   try {
					Thread.sleep(waitTime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
       }
		
	}

}
